package util;

import java.sql.*;

public class JDBCAutoDemo {
	
	static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	static final String DB_URL = "jdbc:mysql://localhost/";

	static final String USER = "";
	static final String PASS = "";
   
	public static void main(String[] args) {
	   
	Connection conn = null;
	Statement stmt = null;
	   
		try {
		   
			//register JDBC driver
			Class.forName("com.mysql.jdbc.Driver");

			//open a connection
			System.out.println("Connecting to a selected database...");
			conn = DriverManager.getConnection(DB_URL, USER, PASS);
			System.out.println("Connected database successfully...");
  
			//execute a query
			stmt = conn.createStatement();
						
			//delete database 'demo'
			System.out.println("Deleting database...");
  			String sql1 = "DROP DATABASE IF EXISTS DEMO";
			stmt.executeUpdate(sql1);
			System.out.println("Database 'demo' deleted successfully...");
  
			//create database 'demo'
			System.out.println("Creating database...");
			String sql2 = "CREATE DATABASE DEMO";
			stmt.executeUpdate(sql2);
			System.out.println("Database 'demo' created successfully...");
			
		} catch (SQLException se) {
			//Handle errors for JDBC
			se.printStackTrace();
		} catch (Exception e) {
			//Handle errors for Class.forName
			e.printStackTrace();
		} finally {
			//finally block used to close resources
		   
			try {
				if (stmt!=null) conn.close();
			} catch (SQLException se) {
	    	  
			}
			
			try {
				if (conn!=null) conn.close();
			} catch (SQLException se) {
				se.printStackTrace();
			}
		}

		System.out.println("Goodbye!\n");

	}
   
}